/*
 * Copyright (C) 2011, 2012 Moravská zemská knihovna v Brně / Moravian Library in Brno
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.oai.provider;

import cz.mzk.kramerius.commons.PropertyLoader;
import cz.mzk.oai.metadataRepository.repository.Repository;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class Configuration {

    //private static String PROPERTIES_FILE = "provider-work.properties";
    //private static String PROPERTIES_FILE = "provider-laptop.properties";
    private static String PROPERTIES_FILE = "provider.properties";
    private static Configuration instance = null;
    private final Repository repository;
    private final String repositoryName;
    private final String baseUrl;
    private final String webUrl;
    private final String earliestDatestamp;
    private final String adminEmail;
    private final int listIdentifiersMaxSize;
    private final int listRecordsMaxSize;
    private final int listRequestsMinutesValid;

    private Configuration() throws IOException {
        PropertyLoader loader = Configuration.getPropertyLoader();
        repository = Repository.instanceOf(loader.getProperties());
        repositoryName = loader.loadString(PropertyNames.REPOSITORY_NAME);
        baseUrl = loader.loadString(PropertyNames.BASE_URL);
        webUrl = loader.loadString(PropertyNames.WEB_URL);
        earliestDatestamp = loader.loadString(PropertyNames.EARLIEST_DATESTAMP);
        adminEmail = loader.loadString(PropertyNames.ADMIN_EMAIL);
        listIdentifiersMaxSize = loader.loadInt(PropertyNames.LIST_IDENTIFIERS_MAX_SIZE);
        listRecordsMaxSize = loader.loadInt(PropertyNames.LIST_RECORDS_MAX_SIZE);
        listRequestsMinutesValid = loader.loadInt(PropertyNames.LIST_REQUESTS_MINUTES_VALID);
    }

    public static PropertyLoader getPropertyLoader() throws IOException {
        InputStream inputStream = Configuration.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE);
        return new PropertyLoader(inputStream);
    }

    public static Configuration instanceOf() throws IOException {
        if (instance == null) {
            instance = new Configuration();
        }
        return instance;
    }

    /**
     * @return the repositoryName
     */
    public String getRepositoryName() {
        return repositoryName;
    }

    /**
     *
     * @return base url
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    /**
     * @return the earliestDatestamp
     */
    public String getEarliestDatestamp() {
        return earliestDatestamp;
    }

    /**
     * @return the adminEmail
     */
    public String getAdminEmail() {
        return adminEmail;
    }

    /**
     * @return the repository
     */
    public Repository getRepository() {
        return repository;
    }

    /**
     * @return the listIdentifiersMaxSize
     */
    public int getListIdentifiersMaxSize() {
        return listIdentifiersMaxSize;
    }

    /**
     * @return the listRecordsMaxSize
     */
    public int getListRecordsMaxSize() {
        return listRecordsMaxSize;
    }

    /**
     *
     * @return
     */
    public String getWebUrl() {
        return webUrl;
    }

    /**
     *
     * @return
     */
    public int getListRequestsMinutesValid() {
        return listRequestsMinutesValid;
    }
}
